import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		content: {
			publishTime: "",
			url: "",
			text: "",
			sourceFrom: "",
			isFavor: "",
			favorIcon: "",
			name: "",
		},
		code: "",
		folderCode: ""
	},
	onLoad(params) {
		var folderCode = params.folderCode == "undefined" ? "" : params.folderCode ;
		var code = params.code;
		this.setData({
			"folderCode": folderCode,
			"code": code,
			"name": params.name,
		});
		//查询基本信息
		this.loadDetail();
		//是否收藏接口
		this.loadFavor();
	},
	back(event) {
		dd.navigateBack({
			delta: 1
		})
	},
	collection(event) {    //收藏

	},
	loadDetail: function(e) {
		//调用内容详情接口,获取内容详情
		var newUrl = getApp().globalData.dangjianUrl + "/content/contentQuery";
		var params = {};
		params["code"] = this.data.code + "";
		params["contentName"] = this.data.name + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params["subCode"] = "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "内容详情加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var content = respData.data.content;
					var thumbnail = content.thumbnail;
					if (null == thumbnail || "" == thumbnail) {
						var imageFiles = content.imageFiles;
						if (null == imageFiles) {
							imageFiles = content.imageFiles;
						}
						if (null != imageFiles && undefined != imageFiles) {
							imageFiles = imageFiles.imageFile;
							for (var j in imageFiles) {
								thumbnail = imageFiles[j].url;
								break;
							}
						}
					}
					this.setData({
						"refCode": content.refCode || '' ,
						"content.publishTime": this.formatDate('yyyy-MM-dd', content.publishTime),
						"content.name": content.name,
						"content.url": thumbnail || '',
						"content.sourceFrom": null == content.sourceFrom || "" == content.sourceFrom ? "华数" : content.sourceFrom
					});
					//调用播放记录查询接口获取图文的正文内容
					this.loadText();

				} else {
          this.showPageMsg("alert", respData.data.result.description)				}
			}
		);
	},

	loadText: function() {
	//	var aParse = require("../aParse/aParse.js");

		var WxParse = require('../wxParse/wxParse.js');
		//获取图文的正文内容
		var newUrl = getApp().globalData.dangjianUrl + "/content/contentUrlQuery";
		var params = {};
		var that = this;
		params["code"] = this.data.code + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params["contentType"] = "1";
		params["from"] = "3";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "图文正文加载失败").then(
			(respData) => {
				console.log(respData)
				var result = respData.data.result.code;
				if (result == 1) {
					var playUrls = respData.data.content.playUrls;
					if (null != playUrls && undefined != playUrls) {
						var text;
						if (playUrls.length > 0) {
							text = playUrls[0].txtValue;
						} else {
							text = '';
						}
						// aParse.aParse("article", "html", text, that, 5);
						WxParse.wxParse("article" , "html", text, that, 5);
						this.setData({
							"content.text": text
						});
					}

				} else {
          this.showPageMsg("alert", respData.data.result.description)				}
			}
		);
	},
	loadFavor: function() {
		//获取是否已经收藏
		var newUrl = getApp().globalData.dangjianUrl + "/member/userContentIsFavor";
		var params = {};
		params["code"] = this.data.code + "";
		params["name"] = this.data.name + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "是否收藏接口调用失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var isFavor = respData.data.favor.isFavor;
					//默认为收藏的状态
					var favorIcon = "../../img/collectFillICON.png";
					if (isFavor == "false") {
						favorIcon = "../../img/collectICON.png";
					}
					this.setData({
						"content.favorIcon": favorIcon,
						"content.isFavor": isFavor
					});
				} else {
					dd.alert({ title: respData.data.result.description });
				}
			}
		);
	},
	favorContent: function(e) {
		//收藏内容
		var status = e.currentTarget.dataset.status;
		var contentName = e.currentTarget.dataset.name;
		status = "false" == status ? 1 : 0;
		var newUrl = getApp().globalData.dangjianUrl + "/member/userContentFavor";
		var params = {};
		params["code"] = this.data.code + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params["status"] = status + "";
		params["contentName"] = contentName + "";
		params["contentType"] = "1";
		params["columnCode"] = this.data.folderCode + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "收藏内容操作失败").then(
			(respData) => { 
				var result = respData.data.result.code;
				var isFavor = "true";
				var favorIcon = "../../img/collectFillICON.png";
				if (1 == result) {
					if (0 == status) {
						favorIcon = "../../img/collectICON.png";
						isFavor = "false";
						this.showPageMsg("tips", "该内容收藏取消成功！");
					} else {
						this.showPageMsg("tips", "该内容收藏成功！");
					}
					this.setData({
						"content.favorIcon": favorIcon,
						"content.isFavor": isFavor
					});
				} else {
          this.showPageMsg("alert", respData.data.result.description)				}
			}
		);
	}
});
